package com.hc.blogssm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hc.blogssm.services.UsersServices;


@Controller
@RequestMapping(value="/users")
public class UsersController {
	@Autowired
	private UsersServices usersServices;
	
	@RequestMapping(value="/login")
	public String login(Map<String,Object> msgMap,@RequestParam String usersName,@RequestParam String passWord,HttpSession session){
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("usersName", usersName);
		map.put("passWord", passWord);
		
		Map<String, Object> loginMap = usersServices.login(map);
		
		if(loginMap==null){
			
			msgMap.put("msgMap", "登录失败");
			return "login";
		}else{
			int id=(Integer)loginMap.get("id");
			session.setAttribute("loginMap", loginMap);
			return "redirect:/blog/findAllContent?usersId="+id+"&pageIndex=1&pageSize=2";
		}
	}
	@RequestMapping(value="/updateById")
	public String updateById(@RequestParam String usersId,@RequestParam String usersName,@RequestParam String passWord){
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("usersName", usersName);
		map.put("passWord", passWord);
		map.put("usersId", usersId);
		
		int updateById = usersServices.updateById(map);
		
		return "login";
	}
	
	@RequestMapping(value="/findAllUsers")
	public String  findAllUsers(Map<String,Object> pageMap,@RequestParam int pageIndex,@RequestParam int pageSize,HttpSession session) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("pageIndex", (pageIndex-1)*pageSize);
		map.put("pageSize", pageSize);
		
		pageMap.put("pageIndex", pageIndex);
		pageMap.put("pageSize", pageSize);
		
		Map<String,Object> countMap = usersServices.findCount(map);
		pageMap.put("countMap",countMap);
		long page=(Long)countMap.get("count");
		pageMap.put("totalPage", page%pageSize==0?(page/pageSize):(page/pageSize+1));
		
		List<Map<String, Object>> usersList = usersServices.findAllUsers(map);
		session.setAttribute("usersList", usersList);
		return "accountAdermin";
	}
	
	@RequestMapping(value="/deleteMsgById")
	public String deleteMsgById(Map<String,Object> msgMap,@RequestParam String id){
		
		Map<String,Object> map= new HashMap<String, Object>();
		
		map.put("id", id);
		
		int deleteMap = usersServices.deleteMsgById(map);
		
		return "redirect:/users/findAllUsers?pageIndex=1&pageSize=3";
	}
	
	@RequestMapping(value="/addAccount")
	public String addAccount(@RequestParam String userName,@RequestParam String mobile,@RequestParam String address,@RequestParam String picpath,@RequestParam String passWord,@RequestParam String age,@RequestParam String sex,@RequestParam String nickName) {
		
		Map<String,Object> map= new HashMap<String, Object>();
		map.put("userName", userName);
		map.put("passWord", passWord);
		map.put("age", age);
		map.put("sex", sex);
		map.put("nickName", nickName);
		map.put("mobile", mobile);
		map.put("address", address);
		map.put("picpath", picpath);
		
		int usersMap=usersServices.addAccount(map);
		
		return "redirect:/users/findAllUsers?pageIndex=1&pageSize=3";
	}
	@RequestMapping(value="/exeit")
	public String exeit(HttpSession session){
		session.invalidate();
		return "login";
	}
}
